//
//  StrategyNearbyTravelNotesTableViewCell.m
//  ITrip
//
//  Created by 1564335 on 15-6-2.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "StrategyNearbyTravelNotesTableViewCell.h"
#import <UIImageView+WebCache.h>

@implementation StrategyNearbyTravelNotesTableViewCell

- (void)dealloc
{
    [_titleLabel release];
    [_image_url release];
    [_imageScrollView release];
//    [_tableView release];
//    [_view release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
    }
    return self;
}

- (void)createCell
{
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.contentView addSubview:self.titleLabel];
    self.imageScrollView = [[UIScrollView alloc] init];
    [self.contentView addSubview:self.imageScrollView];
    self.imageScrollView.delegate = self;
    [self.imageScrollView release];
//    self.view = [[UIView alloc] init];
//    [self addSubview:self.view];
//    self.tableView = [[StrategyNearbyNotesTableView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 150):self.attraction_tripsParser];
    
//    [self.tableView release];
    [self.titleLabel release];
}

- (void)setAttraction_tripsParser:(StrategyAttraction_tripsParser *)attraction_tripsParser
{
    for (UIView *myView in self.imageScrollView.subviews) {
        [myView removeFromSuperview];
    }
    //    self.attraction_tripsParser = attraction_tripsParser;
    if (attraction_tripsParser.comment != nil && ![attraction_tripsParser.comment isEqual:[NSNull null]]) {
        NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18] forKey:NSFontAttributeName];
        CGRect frame = [attraction_tripsParser.comment boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        
        self.titleLabel.frame = CGRectMake(20, 20, self.contentView.frame.size.width - 40, 30);
        self.titleLabel.text = attraction_tripsParser.comment;
        self.titleLabel.numberOfLines = 1000;
        [self.titleLabel sizeToFit];
        CGFloat photosWidth = 0;
        if (attraction_tripsParser.photos_count > 0) {
            int i = 0;
            for (StrategyNotesParser *notesParser in attraction_tripsParser.arrayData) {
                if (notesParser.photo_url != nil && ![notesParser.photo_url isEqual:[NSNull null]] && i++ < 4) {
                    photosWidth += (notesParser.width * 1.0 / (CGFloat)notesParser.height) * 150;
                    
                }
            }
            if (photosWidth > 0) {
//                NSLog(@"%f", photosWidth);
                self.imageScrollView.frame = CGRectMake(20, 40 + frame.size.height, self.contentView.frame.size.width - 40, 150);
                self.imageScrollView.contentSize = CGSizeMake(photosWidth, 150);
                self.imageScrollView.contentOffset = CGPointMake(0, 0);
                CGFloat photoLength = 0;
                int i = 0;
                for (StrategyNotesParser *notesParser in attraction_tripsParser.arrayData) {
                    if (notesParser.width != 0 && notesParser.height != 0 && notesParser.photo_url != nil && ![notesParser.photo_url isEqual:[NSNull null]] && i++ < 4) {
                        photoLength += notesParser.width / (CGFloat)notesParser.height * 150;
                        self.image_url = [[UIImageView alloc] initWithFrame:CGRectMake(photoLength - 150 * notesParser.width / (CGFloat)notesParser.height, 3, 144 * notesParser.width / (CGFloat)notesParser.height, 144)];
                        
                        //                        [self.image_url setImageWithURL:[NSURL URLWithString:notesParser.photo_url] placeholderImage:[UIImage imageNamed:@"defaultPicture"]];
                        [self.image_url sd_setImageWithURL:[NSURL URLWithString:notesParser.photo_url] placeholderImage:[UIImage imageNamed:@"defaultPicture"] options:SDWebImageRefreshCached progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                            
                        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            
                        }];
                        [self.imageScrollView addSubview:self.image_url];
                        [self.image_url release];
                    }
                }
            }
            
        }
    }else if (attraction_tripsParser.comment == nil || [attraction_tripsParser.comment isEqual:[NSNull null]]) {
        if (attraction_tripsParser.photos_count > 0) {
            int i = 0;
            CGFloat photosWidth = 0;
            for (StrategyNotesParser *notesParser in attraction_tripsParser.arrayData) {
                if (notesParser.photo_url != nil && ![notesParser.photo_url isEqual:[NSNull null]] && i++ < 4) {
                    photosWidth += (notesParser.width * 1.0 / (CGFloat)notesParser.height) * 150;
                }
                
            }
            if (photosWidth > 0) {
                self.imageScrollView.frame = CGRectMake(20, 20, self.contentView.frame.size.width - 40, 150);
                self.imageScrollView.contentSize = CGSizeMake(photosWidth, 150);
                self.imageScrollView.contentOffset = CGPointMake(0, 0);
                
                CGFloat photoLength = 0;
                int i = 0;
                for (StrategyNotesParser *notesParser in attraction_tripsParser.arrayData) {
                    if (notesParser.width > 0 && notesParser.height != 0 && ![notesParser.photo_url isEqual:[NSNull null]] && i++ < 4) {
                        photoLength += (notesParser.width / (CGFloat)notesParser.height) * 150;
                        self.image_url = [[UIImageView alloc] initWithFrame:CGRectMake(3 + photoLength - 150 * notesParser.width / (CGFloat)notesParser.height, 3, 144 * notesParser.width / (CGFloat)notesParser.height, 144)];
                        
                        //                        [self.image_url setImageWithURL:[NSURL URLWithString:notesParser.photo_url] placeholderImage:[UIImage imageNamed:@"defaultPicture"]];
                        [self.image_url sd_setImageWithURL:[NSURL URLWithString:notesParser.photo_url] placeholderImage:[UIImage imageNamed:@"defaultPicture"] options:SDWebImageRefreshCached];
                        
                        [self.imageScrollView addSubview:self.image_url];
                        [self.image_url release];
                    }
                }
            }
            
        }
        
    }
}


//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    [[SDImageCache sharedImageCache] clearMemory];
//}


//        if (attraction_tripsParser.photos_count > 0 && photosWidth > 0) {
//            self.view.frame = CGRectMake(60, frame.size.height - 35, self.frame.size.width, 150) ;
//            self.tableView = [[StrategyNearbyNotesTableView alloc] initWithFrame:CGRectMake(0, 0, 150, self.frame.size.width):attraction_tripsParser];
//            [self.view addSubview:self.tableView];
//            [self.tableView release];
//        }
//    } else if (attraction_tripsParser.comment == nil || [attraction_tripsParser.comment isEqual:[NSNull null]]) {
//        CGFloat photosWidth = 0;
//        if (attraction_tripsParser.photos_count > 0) {
//            for (StrategyNotesParser *notesParser in attraction_tripsParser.arrayData) {
//                if (notesParser.photo_url != nil && ![notesParser.photo_url isEqual:[NSNull null]]) {
//                    photosWidth += (notesParser.width * 1.0 / (CGFloat)notesParser.height) * 150;
//                }
//            }
//        }
//        if (attraction_tripsParser.photos_count > 0 && photosWidth > 0) {
//            self.view.frame = CGRectMake(60, -50, self.frame.size.width, 150) ;
//            self.tableView = [[StrategyNearbyNotesTableView alloc] initWithFrame:CGRectMake(0, 0, 150, self.frame.size.width):attraction_tripsParser];
//            [self.view addSubview:self.tableView];
//            [self.tableView release];
//        }
//    }

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
